package com.sakura.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sakura.common.R;
import com.sakura.pojo.Category;
import com.sakura.serivce.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> add(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Category>> selectPage(@RequestParam("page") Integer page,
                                        @RequestParam("pageSize") Integer pageSie) {
        // 存入分页条件
        Page<Category> pageInfo = new Page<>(page, pageSie);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort").orderByDesc("create_time");
        categoryService.page(pageInfo, wrapper);

        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam("id") Long id) {
        categoryService.remove(id);
        return R.success("删除成功");
    }

    @GetMapping("/list")
    public R<List<Category>> selectClassify(@RequestParam(required = false,value = "type") Integer type){
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq(type != null,"type",type);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
